package com.hiwan.wechat.service;

import java.util.List;

import com.hiwan.system.util.HttpUtil;
import com.hiwan.wechat.pojo.NewsMessage;
import com.hiwan.wechat.pojo.NewsMessage.Article;
import com.hiwan.wechat.quartz.AccessTokenQuartz;
import com.hiwan.wechat.pojo.TextMessage;
import com.hiwan.wechat.pojo.TextcardMessage;
import com.hiwan.wechat.util.WechatRequestUrl;

import net.sf.json.JSONObject;


	
/**
 * 微信企业号
 * 通讯录数据同步
* @ClassName: MGroup
* @Description: 
* @author zouqin
* @date 2016年3月9日 下午3:21:44
 */
public class PublishMessageService {
	
	public static final long maxUserNum = 1000;
	
	public static final String separatorChar = "|";
	
	public static final long maxPartyNum = 100;
	/**
	 *  群发接口
	 * @param isToAll true：群发所有人，tousers、topartys、totags传值null。
	 * @param tousers 用户userid列表,最多支持1000个,|分隔符
	 * @param topartys 用户所在部门列表,最多支持100个,|分隔符
	 * @param totags 用户所在标签列表，目前传null
	 * @param agentid 应用id
	 * @param articles,支持1到10条图文
	 * @return
	 */
	public static String publishNewsMsg(String accessToken, boolean isToAll, String tousers, String topartys, String totags, long agentid, Article article){
		try {
			NewsMessage newsMessage = new NewsMessage();
			newsMessage.setAgentid(agentid);
			
			if (isToAll){
				newsMessage.setTouser("@all");
			}else{
				if (tousers != null && !tousers.equals("")){
					newsMessage.setTouser(tousers);
				}
				if (topartys != null && !topartys.equals("")){
					newsMessage.setToparty(topartys);
				}
				if (totags != null && !totags.equals("")){
					newsMessage.setTotag(totags);
				}
			}
			
			newsMessage.getNews().getArticles().add(article);
			
			System.out.println("sendMessage >> "+newsMessage.parseToJsonStr());
			String requestUrl = WechatRequestUrl.SEND_MESSAGE_URL.replace("ACCESS_TOKEN", accessToken);
			JSONObject jsonobject = HttpUtil.httpRequest(requestUrl, "POST", newsMessage.parseToJsonStr());
			return jsonobject.toString();
		} catch (Exception e) { 
			e.printStackTrace();
		} 
		return null;
	}
	
	public static String publishNewsMsg(String accessToken, boolean isToAll, String tousers, String topartys, String totags, long agentid, List<Article> articles){
		try {
			NewsMessage newsMessage = new NewsMessage();
			newsMessage.setAgentid(agentid);
			
			if (isToAll){
				newsMessage.setTouser("@all");
			}else{
				if (tousers != null && !tousers.equals("")){
					newsMessage.setTouser(tousers);
				}
				if (topartys != null && !topartys.equals("")){
					newsMessage.setToparty(topartys);
				}
				if (totags != null && !totags.equals("")){
					newsMessage.setTotag(totags);
				}
			}
			
			newsMessage.getNews().setArticles(articles);
			
			System.out.println("sendMessage >> "+newsMessage.parseToJsonStr());
			String requestUrl = WechatRequestUrl.SEND_MESSAGE_URL.replace("ACCESS_TOKEN", accessToken);
			JSONObject jsonobject = HttpUtil.httpRequest(requestUrl, "POST", newsMessage.parseToJsonStr());
			System.out.println(jsonobject.toString());
			return jsonobject.toString();
		} catch (Exception e) { 
			e.printStackTrace();
		} 
		return null;
	}

	/**
	 *  群发textcard消息接口
	 * @param isToAll true：群发所有人，tousers、topartys、totags传值null。
	 * @param tousers 用户userid列表,最多支持1000个,|分隔符
	 * @param topartys 用户所在部门列表,最多支持100个,|分隔符
	 * @param totags 用户所在标签列表，目前传null
	 * @param agentid 应用id
	 * @param textcard,消息内容
	 * @return
	 */
	public static String publishTextcardMsg(String accessToken, boolean isToAll, String tousers, String topartys, String totags, long agentid, String title, String description, String url, String btntxt){
		try {
			TextcardMessage textcardMessage = new TextcardMessage();
			textcardMessage.setAgentid(agentid);
			
			if (isToAll){
				textcardMessage.setTouser("@all");
			}else{
				if (tousers != null && !tousers.equals("")){
					textcardMessage.setTouser(tousers);
				}
				if (topartys != null && !topartys.equals("")){
					textcardMessage.setToparty(topartys);
				}
				if (totags != null && !totags.equals("")){
					textcardMessage.setTotag(totags);
				}
			}
			textcardMessage.setTextcard(textcardMessage.new Textcard(title, description, url, btntxt));
			
			System.out.println("sendMessage >> " + textcardMessage.parseToJsonStr());
			String requestUrl = WechatRequestUrl.SEND_MESSAGE_URL.replace("ACCESS_TOKEN", accessToken);
			JSONObject jsonobject = HttpUtil.httpRequest(requestUrl, "POST", textcardMessage.parseToJsonStr());
			return jsonobject.toString();
		} catch (Exception e) { 
			e.printStackTrace();
		} 
		return null;
	}
	
	/**
	 *  群发text消息接口
	 * @param isToAll true：群发所有人，tousers、topartys、totags传值null。
	 * @param tousers 用户userid列表,最多支持1000个,|分隔符
	 * @param topartys 用户所在部门列表,最多支持100个,|分隔符
	 * @param totags 用户所在标签列表，目前传null
	 * @param agentid 应用id
	 * @param content,消息内容
	 * @return
	 */
	public static String publishTextMsg(String accessToken, boolean isToAll, String tousers, String topartys, String totags, long agentid, String content, String safe){
		try {
			TextMessage textMessage = new TextMessage();
			textMessage.setAgentid(agentid);
			
			if (isToAll){
				textMessage.setTouser("@all");
			}else{
				if (tousers != null && !tousers.equals("")){
					textMessage.setTouser(tousers);
				}
				if (topartys != null && !topartys.equals("")){
					textMessage.setToparty(topartys);
				}
				if (totags != null && !totags.equals("")){
					textMessage.setTotag(totags);
				}
			}
			textMessage.setText(textMessage.new Text(content));
			textMessage.setSafe(safe);
			
			System.out.println("sendMessage >> " + textMessage.parseToJsonStr());
			String requestUrl = WechatRequestUrl.SEND_MESSAGE_URL.replace("ACCESS_TOKEN", accessToken);
			JSONObject jsonobject = HttpUtil.httpRequest(requestUrl, "POST", textMessage.parseToJsonStr());
			return jsonobject.toString();
		} catch (Exception e) { 
			e.printStackTrace();
		} 
		return null;
	}
	
	/**
	 * 用户列表 推送信息
	 * @param accessToken  
	 * @param agentid      8
	 * @param isToAll
	 * @param tousers
	 * @param articles
	 */
	public static void publishExamToUsers(String accessToken, long agentid, boolean isToAll, List<String> tousers, List<Article> articles){
		if(null == tousers){
			return;
		}
		StringBuffer sb = new StringBuffer();
		for(int i = 0; i < tousers.size(); i++){
			sb.append("|" + tousers.get(i));
			if (0 == i % maxUserNum || i == tousers.size() - 1){
				PublishMessageService.publishNewsMsg(accessToken, isToAll, sb.toString().substring(1), null, null, agentid, articles);
				sb = new StringBuffer();
			}else{
				sb.append(separatorChar);
			}
		}
	}
	

}
